package org.swim.web.utils;

import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;

public class JsonUtils {

	public static JsonConfig createJsonParamsFilter(String... strings) {
		JsonConfig jsonCfg = new JsonConfig();

		final List<String> strs = Arrays.asList(strings);

		jsonCfg.setJsonPropertyFilter(new PropertyFilter() {

			public boolean apply(Object arg0, String arg1, Object arg2) {

				if (strs.contains(arg1)) {
					return false;
				}

				return true;
			}
		});

		return jsonCfg;
	}

	public static JsonValueProcessor getJvpStringArrToString(
			String... arrayAttributes) {

		JsonValueProcessor JVP;

		final List<String> arrayAttributesList = Arrays.asList(arrayAttributes);

		JVP = new JsonValueProcessor() {

			public Object processObjectValue(String key, Object arg1,
					JsonConfig arg2) {
				if (!arrayAttributesList.contains(key)) {
					return ((String[]) arg1)[0];
				} else {
					return JSONArray.fromObject(arg1);
				}

			}

			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}
		};

		return JVP;
	}

}
